package com.xyy.kingdeejdy.helper;

import com.google.common.collect.Lists;
import com.xyy.kingdeejdy.pojo.erp.customer.JdCustomerInfo;
import com.xyy.kingdeejdy.pojo.erp.product.JdProductStockDto;
import com.xyy.kingdeejdy.pojo.pop.PopErpClientInfoDto;
import com.xyy.kingdeejdy.pojo.pop.PopErpSkuStockV2Dto;
import org.apache.commons.collections4.MapUtils;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;

public class JdHelper {


    public static PopErpSkuStockV2Dto convertPopStock(String key, List<JdProductStockDto> value) {
        PopErpSkuStockV2Dto popErpSkuStockV2Dto = new PopErpSkuStockV2Dto();
        popErpSkuStockV2Dto.setErpCode(key);
        String minProductDate = value.stream().filter(m -> m.getQty() > 0).map(item -> item.getProdDate()).min(String::compareTo).orElse(null);
        String maxProductDate = value.stream().filter(m -> m.getQty() > 0).map(item -> item.getProdDate()).filter(Objects::nonNull).max(String::compareTo).orElse(null);
        String minValidDate = value.stream().filter(m -> m.getQty() > 0).map(item -> item.getValidDate()).filter(Objects::nonNull).min(String::compareTo).orElse(null);
        String maxValidDate = value.stream().filter(m -> m.getQty() > 0).map(item -> item.getValidDate()).filter(Objects::nonNull).max(String::compareTo).orElse(null);
        Integer stockTotal = value.stream().map(m -> m.getQty()).filter(Objects::nonNull).reduce(Integer::sum).get();
        popErpSkuStockV2Dto.setOldestProDate(minProductDate);
        popErpSkuStockV2Dto.setNewProDate(maxProductDate);
        popErpSkuStockV2Dto.setNearEffect(minValidDate);
        popErpSkuStockV2Dto.setFarEffect(maxValidDate);
        popErpSkuStockV2Dto.setAvailableQyt(stockTotal);
        return popErpSkuStockV2Dto;
    }

    public static List<PopErpSkuStockV2Dto> convertToPopErpSkuStockDtos(Map<String, List<JdProductStockDto>> pidStockMap) {
        if (MapUtils.isEmpty(pidStockMap)) {
            return Lists.newArrayList();
        }
        List<PopErpSkuStockV2Dto> popErpClientInfoDtolist = Lists.newArrayList();
        for (Map.Entry<String, List<JdProductStockDto>> entry : pidStockMap.entrySet()) {
            popErpClientInfoDtolist.add(JdHelper.convertPopStock(entry.getKey(), entry.getValue()));
        }
        return popErpClientInfoDtolist;
    }

    public static List<PopErpClientInfoDto> convertToPopErpClientInfoDtos(String orgId, List<JdCustomerInfo> rows) {
        if (CollectionUtils.isEmpty(rows)) {
            return Lists.newArrayList();
        }
        List<PopErpClientInfoDto> popErpClientInfoDtolist = Lists.newArrayList();
        for (JdCustomerInfo customerHXPo : rows) {
            popErpClientInfoDtolist.add(convertFromCustomerHXPo(orgId, customerHXPo));
        }
        return popErpClientInfoDtolist;
    }

    private static PopErpClientInfoDto convertFromCustomerHXPo(String orgId, JdCustomerInfo customerHXPo) {
        PopErpClientInfoDto popErpClientInfoDto = new PopErpClientInfoDto();
        popErpClientInfoDto.setCustomerId(customerHXPo.getId());
        popErpClientInfoDto.setCustomerCode(customerHXPo.getNumber());
        popErpClientInfoDto.setCustomerName(customerHXPo.getName());
        popErpClientInfoDto.setProvinceId(null);
        popErpClientInfoDto.setProvinceName(null);
        popErpClientInfoDto.setCityId(null);
        popErpClientInfoDto.setCityName(null);
        popErpClientInfoDto.setCountryId(null);
        popErpClientInfoDto.setCountryName(null);
        return popErpClientInfoDto;
    }
}
